{
 "cells": [
  {
   "cell_type": "markdown",
   "id": "6082a52f",
   "metadata": {},
   "source": [
    "# 实践face++，人脸API第一步，获取人脸face_taken"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 22,
   "id": "5382d9f6",
   "metadata": {},
   "outputs": [],
   "source": [
    "# 1. 导入需要的requests模块\n",
    "import requests\n",
    "\n",
    "# 2. 输入我们需要API网站注册的API_Key,api账号/通行证\n",
    "# API_Key = \"自己的API_Key\"\n",
    "# API_secret = \"自己的API_secret\"\n",
    "\n",
    "# 3. 准备API测试接口\n",
    "face_detect = \"https://api-cn.faceplusplus.com/facepp/v3/detect\"\n",
    "    \n",
    "# 4. 调用方法POST\n",
    "\n",
    "# 5. 准备url传递参数\n",
    "payload = {\n",
    "    'api_key':'lF2JmrX78SS6-VERJTIAolLXm8_B6Fv8',\n",
    "    'api_secret':'3tIf7ThN8STzEisCsN277l2-AYXwD_pq',\n",
    "    'image_url':'https://img.zcool.cn/community/0171c259083a1fa801214550f2a1d3.jpg@1280w_1l_2o_100sh.jpg',\n",
    "    'return_attributes':'gender,age,smiling'\n",
    "}\n",
    "\n",
    "# 6. 发起请求\n",
    "r = requests.post(url=face_detect,params=payload)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 23,
   "id": "5b37e764",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "<Response [200]>"
      ]
     },
     "execution_count": 23,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "r"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 24,
   "id": "fa0eb729",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "{'request_id': '1647915349,5f90bff0-0299-4829-93d2-418b15dd71ac',\n",
       " 'time_used': 507,\n",
       " 'faces': [{'face_token': '15ff682a3d53318eb258d506b4361dd3',\n",
       "   'face_rectangle': {'top': 481, 'left': 279, 'width': 748, 'height': 748},\n",
       "   'attributes': {'gender': {'value': 'Female'},\n",
       "    'age': {'value': 25},\n",
       "    'smile': {'value': 100.0, 'threshold': 50.0}}}],\n",
       " 'image_id': 'bxJ7Mcl+EfZY1aIF7zt3dA==',\n",
       " 'face_num': 1}"
      ]
     },
     "execution_count": 24,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "r.json()"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "9f9a80ec",
   "metadata": {},
   "source": [
    "* payloadabs传递参数\n",
    "* ? 标记+参数（变量赋值进行参数传递，参数可多个，长度有限制）"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "0fabd1d1",
   "metadata": {},
   "source": [
    "# 1.人脸分析API"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 25,
   "id": "e63f6f75",
   "metadata": {},
   "outputs": [],
   "source": [
    "\n",
    "face_detect = \"https://api-cn.faceplusplus.com/facepp/v3/face/analyze\"\n",
    "\n",
    "payload = {\n",
    "    'api_key':'lF2JmrX78SS6-VERJTIAolLXm8_B6Fv8',\n",
    "    'api_secret':'3tIf7ThN8STzEisCsN277l2-AYXwD_pq',\n",
    "    'face_tokens':'1382c73e7e15a5ec016e5591749107af',\n",
    "    'return_attributes':'gender,age,smiling'\n",
    "}\n",
    "\n",
    "r = requests.post(url=face_detect,params=payload)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 26,
   "id": "b1bf3710",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "<Response [200]>"
      ]
     },
     "execution_count": 26,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "r"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 27,
   "id": "d11b4764",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "{'time_used': 305,\n",
       " 'request_id': '1647915352,b55bfdff-398f-45aa-9900-bf301631b862',\n",
       " 'faces': [{'attributes': {'gender': {'value': 'Female'},\n",
       "    'age': {'value': 24},\n",
       "    'smile': {'threshold': 50.0, 'value': 100.0}},\n",
       "   'face_rectangle': {'width': 748, 'top': 481, 'left': 279, 'height': 748},\n",
       "   'face_token': '1382c73e7e15a5ec016e5591749107af'}]}"
      ]
     },
     "execution_count": 27,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "r.json()"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "2996f798",
   "metadata": {},
   "source": [
    "# 2. 人脸对比"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 28,
   "id": "f780fdaa",
   "metadata": {},
   "outputs": [],
   "source": [
    "# 1. 导入需要的requests模块\n",
    "import requests\n",
    "\n",
    "# 2. 输入我们需要API网站注册的API_Key,api账号/通行证\n",
    "# API_Key = \"自己的API_Key\"\n",
    "# API_secret = \"自己的API_secret\"\n",
    "\n",
    "# 3. 准备API测试接口\n",
    "face_detect = \"https://api-cn.faceplusplus.com/facepp/v3/compaer\"\n",
    "    \n",
    "# 4. 调用方法POST\n",
    "\n",
    "# 5. 准备url传递参数\n",
    "payload = {\n",
    "    'api_key':'lF2JmrX78SS6-VERJTIAolLXm8_B6Fv8',\n",
    "    'api_secret':'3tIf7ThN8STzEisCsN277l2-AYXwD_pq',\n",
    "    'image_url':'https://img.zcool.cn/community/0140155814ae87a84a0d304f690ebb.jpg@1280w_1l_2o_100sh.jpg',\n",
    "    'return_attributes':'gender,age,smiling'\n",
    "}\n",
    "\n",
    "# 6. 发起请求\n",
    "r = requests.post(url=face_detect,params=payload)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 29,
   "id": "467d9839",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "{'error_message': 'API_NOT_FOUND'}"
      ]
     },
     "execution_count": 29,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "r.json()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 30,
   "id": "aabf930c",
   "metadata": {},
   "outputs": [],
   "source": [
    "\n",
    "face_detect = \"https://api-cn.faceplusplus.com/facepp/v3/compare\"\n",
    "\n",
    "payload = {\n",
    "    'api_key':'lF2JmrX78SS6-VERJTIAolLXm8_B6Fv8',\n",
    "    'api_secret':'3tIf7ThN8STzEisCsN277l2-AYXwD_pq',\n",
    "    'face_token1':'1382c73e7e15a5ec016e5591749107af',\n",
    "    'face_token2':'ecac14f529a59e66998fa2d8923f48b0'\n",
    "}\n",
    "\n",
    "r = requests.post(url=face_detect,params=payload)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 31,
   "id": "2cfa5f07",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "<Response [200]>"
      ]
     },
     "execution_count": 31,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "r"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 32,
   "id": "016cdad5",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "{'request_id': '1647915353,6ea824cd-1bc4-4913-9f24-e22f261aa65b',\n",
       " 'time_used': 513,\n",
       " 'confidence': 33.636,\n",
       " 'thresholds': {'1e-3': 62.327, '1e-4': 69.101, '1e-5': 73.975}}"
      ]
     },
     "execution_count": 32,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "r.json()"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "411a6466",
   "metadata": {},
   "source": [
    "# 3. 封装应用参数（内部使用的接口）"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "596386f6",
   "metadata": {},
   "source": [
    "## 1. 人脸检测"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 33,
   "id": "ca7242be",
   "metadata": {},
   "outputs": [],
   "source": [
    "# 准备工作\n",
    "API_key = 'lF2JmrX78SS6-VERJTIAolLXm8_B6Fv8'\n",
    "API_sercret = '3tIf7ThN8STzEisCsN277l2-AYXwD_pq'"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 34,
   "id": "19a709a2",
   "metadata": {},
   "outputs": [],
   "source": [
    "def face_detect(API_key,API_sercret,image_url,landmark=None,attributes=None):\n",
    "    \"\"\"face++ 人脸检测\"\"\"\n",
    "    import requests\n",
    "    url = 'https://api-cn.faceplusplus.com/facepp/v3/detect'\n",
    "    payload ={\n",
    "        'api_key':API_key,\n",
    "        'api_secret':API_sercret,\n",
    "        'image_url':image_url,\n",
    "        'return_landmark':landmark,\n",
    "        'return_attributes':attributes\n",
    "    }\n",
    "    r = requests.post(url,params=payload)\n",
    "    return r.json()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 35,
   "id": "b0c272a5",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "<Response [200]>"
      ]
     },
     "execution_count": 35,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "r"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 36,
   "id": "ee444691",
   "metadata": {},
   "outputs": [],
   "source": [
    "face01 = face_detect(API_key,API_sercret,'https://img.zcool.cn/community/0140155814ae87a84a0d304f690ebb.jpg@1280w_1l_2o_100sh.jpg')"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 37,
   "id": "c7340f40",
   "metadata": {},
   "outputs": [],
   "source": [
    "face02 = face_detect(API_key,API_sercret,'https://img.zcool.cn/community/0171c259083a1fa801214550f2a1d3.jpg@1280w_1l_2o_100sh.jpg')"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 38,
   "id": "221b2a8c",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "'a73e93533871d1ce7fbe8b300981c377'"
      ]
     },
     "execution_count": 38,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "face01['faces'][0]['face_token']"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 39,
   "id": "fd3babc3",
   "metadata": {
    "scrolled": true
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "'24219a1b4b4aebf90f4b3d20afb7806f'"
      ]
     },
     "execution_count": 39,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "face02['faces'][0]['face_token']"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "3ca4bc94",
   "metadata": {},
   "source": [
    "# 作业"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "f32b2baf",
   "metadata": {},
   "source": [
    "## 2.封装人脸分析"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 40,
   "id": "c49f16d5",
   "metadata": {},
   "outputs": [],
   "source": [
    "def face_analyze(API_key,API_sercret,face_token,attributes,landmark=None):\n",
    "    \"\"\"face++ 人脸分析\"\"\"\n",
    "    import requests\n",
    "    url = 'https://api-cn.faceplusplus.com/facepp/v3/face/analyze'\n",
    "    payload ={\n",
    "        'api_key':API_key,\n",
    "        'api_secret':API_sercret,\n",
    "        'face_token':face_token,\n",
    "        'return_landmark':landmark,\n",
    "        'return_attributes':attributes\n",
    "    }\n",
    "    r = requests.post(url,params=payload)\n",
    "    return r.json()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 41,
   "id": "49a87903",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "<Response [200]>"
      ]
     },
     "execution_count": 41,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "r"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 42,
   "id": "b05c38e5",
   "metadata": {},
   "outputs": [],
   "source": [
    "face03 = face_analyze(API_key,API_sercret,'bfd841bee3c2544887000cf6d35339dd','gender,age,smiling')"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 43,
   "id": "21407224",
   "metadata": {},
   "outputs": [],
   "source": [
    "face04 = face_analyze(API_key,API_sercret,'1eb9ddfd095754876d92a08fbeeedcc2','gender,age,smiling')"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "6cd1c8c0",
   "metadata": {},
   "outputs": [],
   "source": []
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "f9bb4014",
   "metadata": {},
   "outputs": [],
   "source": []
  },
  {
   "cell_type": "markdown",
   "id": "b183e21f",
   "metadata": {},
   "source": [
    "## 3.封装人脸对比"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 44,
   "id": "30bdbc38",
   "metadata": {},
   "outputs": [],
   "source": [
    "def face_compare(API_key,API_sercret,face_token,attributes=None,landmark=None):\n",
    "    \"\"\"face++ 人脸对比\"\"\"\n",
    "    import requests\n",
    "    url = 'https://api-cn.faceplusplus.com/facepp/v3/compare'\n",
    "    payload ={\n",
    "        'api_key':API_key,\n",
    "        'api_secret':API_sercret,\n",
    "        'face_token':face_token,\n",
    "        'return_landmark':landmark,\n",
    "        'return_attributes':attributes\n",
    "    }\n",
    "    r = requests.post(url,params=payload)\n",
    "    return r.json()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 45,
   "id": "f94f19f2",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "<Response [200]>"
      ]
     },
     "execution_count": 45,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "r"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "91668a59",
   "metadata": {},
   "source": [
    "## 4.人脸搜索"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 58,
   "id": "f9f93475",
   "metadata": {},
   "outputs": [],
   "source": [
    "face_search = \"https://api-cn.faceplusplus.com/facepp/v3/search\"\n",
    "\n",
    "payload = {\n",
    "    'api_key':'lF2JmrX78SS6-VERJTIAolLXm8_B6Fv8',\n",
    "    'api_secret':'3tIf7ThN8STzEisCsN277l2-AYXwD_pq',\n",
    "    'face_token': '24219a1b4b4aebf90f4b3d20afb7806f',\n",
    "    'faceset_token': '4deb65d367807e027e48e318b17572c7'\n",
    "}\n",
    "\n",
    "r = requests.post(url=face_search,params=payload)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 59,
   "id": "7398e323",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "<Response [200]>"
      ]
     },
     "execution_count": 59,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "r"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 60,
   "id": "b8ec98e2",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "{'request_id': '1647915592,d53e54f1-1a5f-4265-b6b2-bf782234ae07',\n",
       " 'time_used': 541,\n",
       " 'thresholds': {'1e-3': 62.327, '1e-5': 73.975, '1e-4': 69.101},\n",
       " 'results': [{'confidence': 97.042,\n",
       "   'user_id': '',\n",
       "   'face_token': '24219a1b4b4aebf90f4b3d20afb7806f'}]}"
      ]
     },
     "execution_count": 60,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "r.json()"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "18b9be02",
   "metadata": {},
   "source": [
    "## 创建数据库（first）"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 49,
   "id": "c88b61a1",
   "metadata": {},
   "outputs": [],
   "source": [
    "face_creat = \"https://api-cn.faceplusplus.com/facepp/v3/faceset/create\"\n",
    "\n",
    "payload = {\n",
    "    'api_key':'lF2JmrX78SS6-VERJTIAolLXm8_B6Fv8',\n",
    "    'api_secret':'3tIf7ThN8STzEisCsN277l2-AYXwD_pq',\n",
    "    'face_tokens':'24219a1b4b4aebf90f4b3d20afb7806f'\n",
    "}\n",
    "\n",
    "r = requests.post(url=face_creat,params=payload)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 50,
   "id": "2b04eb83",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "<Response [200]>"
      ]
     },
     "execution_count": 50,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "r"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 51,
   "id": "4a7bc750",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "{'faceset_token': '4deb65d367807e027e48e318b17572c7',\n",
       " 'time_used': 606,\n",
       " 'face_count': 1,\n",
       " 'face_added': 1,\n",
       " 'request_id': '1647915418,6e92a3e5-6cdb-47ff-881c-93dd863a86aa',\n",
       " 'outer_id': '',\n",
       " 'failure_detail': []}"
      ]
     },
     "execution_count": 51,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "r.json()"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.8.8"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}
